package com.uit.project.bustrip;

import java.util.ArrayList;


import com.uit.project.bustrip.adapter.ListSeatAdapter;
import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bo.CustomerBOImpl;
import com.uit.project.bustrip.bo.SeatBOImpl;
import com.uit.project.bustrip.bo.TicketBOImpl;
import com.uit.project.bustrip.bointerface.SeatBO;
import com.uit.project.bustrip.bointerface.TicketBO;
import com.uit.project.bustrip.entity.Customer;
import com.uit.project.bustrip.entity.Seat;
import com.uit.project.bustrip.entity.Ticket;
import com.uit.project.bustrip.valueobject.JSONResult;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ChoseSeat extends Activity {
	GridView gridView;
	ArrayList<Seat> items;
	Integer tripId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chose_seat);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
		.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		gridView = (GridView) findViewById(R.id.gridSeat);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			tripId = extras.getInt("tripid");
			SeatBO seatBO = new SeatBOImpl(this);
			items = (ArrayList<Seat>) seatBO.getAllSeatByTripAsync(tripId);
			gridView.setAdapter(new ListSeatAdapter(this, items));
			gridView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					CustomerBOImpl customerBO = new CustomerBOImpl(getApplicationContext());
					Customer customer = customerBO.getInstance();
					if(customer != null){
						Seat item = items.get(position);
						Ticket ticket = new Ticket();
						ticket.trip = item.trip;
						ticket.customer = customer.id;
						ticket.seat = item.id;
						TicketBO ticketBO = new TicketBOImpl(getApplicationContext());
						JSONResult jsonResult = ticketBO.order(ticket);
						afterOrder(jsonResult);
					}
				}
			});
		}
	}
	private void afterOrder(JSONResult jsonResult){
		if(jsonResult == null){
			Toast.makeText(getApplicationContext(), AppsConfig.MESSAGE_CONNECTION_ERROR, Toast.LENGTH_SHORT).show();
			return;
		}
		if(jsonResult.getResult().equals(AppsConfig.RESULT_FAIL)){
			Toast.makeText(getApplicationContext(), jsonResult.getMessage(), Toast.LENGTH_SHORT).show();
		}
		else{
			Toast.makeText(getApplicationContext(), jsonResult.getMessage(), Toast.LENGTH_SHORT).show();
			String msg = "Your ticket code is " + jsonResult.getData().toString();
			String msg1 = "Please use this on your trip";
			Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
			Toast.makeText(getApplicationContext(), msg1, Toast.LENGTH_SHORT).show();
			Intent i = new Intent(getApplicationContext(),ListPoint.class);
			i.putExtra("tripID", tripId);
			startActivity(i);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chose_seat, menu);
		return true;
	}

}
